var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    location:{},
    locationList:[],
    selectIndex:null,
    edit:false
  },
  valueChange: function (e) {
    var value = e.detail.value;
    this.setData({"location.address":value});
  },
  openLocation:function(e){
    var index = e.currentTarget.dataset.index;
    var location = this.data.locationList[index];
    wx.openLocation({
      latitude: location.latitude,
      longitude: location.longitude,
    })
  },
  chooseLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        var data = that.data.location;
        data.name = res.name;
        data.latitude = res.latitude;
        data.longitude = res.longitude;
        that.setData({ "location": data });
      },
    })
    
  },
  saveOrUpdate:function(){
    //检测地址
    if (util.isBlank(this.data.location.name)) {
      util.showTopTips("请选择地理位置");
      return false;
    }
    if (util.isBlank(this.data.location.address)) {
      util.showTopTips("取货地点不能为空");
      return false;
    }
    var location=this.data.location;
    var that=this;
    util.post({
      uri: "/pub/location/save", 
      params: location, 
      contentTypeJson:true,
      success: function(r){
        var list = that.data.locationList;
        if (location.locationId==undefined){
          location.locationId=r.locationId;
          list.push(location);
        }else{
          list[that.data.selectIndex]=location;
        }
        that.setData({"locationList":list,"edit":false});
      }
    });
  },
  delItem:function(){
    if (this.data.selectIndex == null) {
      wx.showModal({
        content: '请选择地址',
        showCancel: false
      });
      return false;
    }
    var that = this;
    wx.showModal({
      content: '确定删除该数据？',
      success(res) {
        if (res.confirm) {
          that.del();
        }
      }
    })
  },
  del: function () {
    var location = this.data.locationList[this.data.selectIndex];
    var that = this;
    util.post({
      uri: "/pub/location/delete",
      params: { "locationId": location.locationId},
      contentTypeJson: true,
      success: function (r) {
        var list = that.data.locationList;
        list.splice(that.data.selectIndex,1);
        that.setData({ "locationList": list});
      }
    });
  },
  cancel:function(){
    this.setData({edit: false});
  },
  selectOK:function(){
    if (this.data.selectIndex == null){
      wx.showModal({
        content: '请选择地址',
        showCancel:false
      });
      return false;
    }
    var location = this.data.locationList[this.data.selectIndex];
    var pages = getCurrentPages();
    pages[pages.length - 2].setData({ "location": location, "groupInfo.locationId": location.locationId});
    wx.navigateBack({
      delta: 1
    })
  },
  viewInfo:function(){
    if (this.data.selectIndex==null) {
      wx.showModal({
        content: '请选择地址',
        showCancel: false
      });
      return false;
    }
    var location = this.data.locationList[this.data.selectIndex];
    this.setData({ location: location, edit: true });
  },
  add:function(){
    var list = this.data.locationList;
    for (var i = 0; i < list.length; i++) {
      list[i].checked = false;
    }
    this.setData({ location: {}, locationList: list, selectIndex: null, edit: true });
  },
  itemClick:function(e){
    var index=e.currentTarget.dataset.index;
    if (this.data.selectIndex == index){
      return;
    }
    var list = this.data.locationList;
    for(var i=0;i<list.length;i++){
      if(i==index){
        list[i].checked=true;
      }else{
        list[i].checked = false;
      }
    }
    this.setData({ locationList: list, selectIndex: index});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var selectId=options.selectId;
    var that=this;
    util.gets({
      uri:"/pub/location/list",
      success:function(r){
        var list=r.page.list;
        var index=null;
        if(selectId){
          for(var i=0;i<list.length;i++){
            if(list[i].locationId==selectId){
              list[i].checked=true;
              index=i;
              break;
            }
          }
        }
        that.setData({ "locationList": list, "selectIndex": index});
      }
    });
    wx.setNavigationBarTitle({
      title: '地址管理',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})